/*
 * 类名称:AddressCityServiceImpl.java
 * 包名称:com.lht.service.impl
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2019-09-30 17:02:56        ZhengZhanWu     初版做成
 */
package com.lht.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lht.admin.cache.J2CacheUtils;
import com.lht.admin.constants.DataValidConstant;
import com.lht.admin.constants.RedisKeysConstant;
import com.lht.admin.dao.AddressCityDao;
import com.lht.admin.entity.AddressCityEntity;
import com.lht.admin.service.AddressCityService;
import com.lht.admin.utils.QueryPlus;
import com.lht.admin.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 城市表Service实现类
 *
 * @author ZhengZhanWu
 * @date 2019-09-30 17:02:56
 */
@Service("addressCityService")
public class AddressCityServiceImpl extends ServiceImpl<AddressCityDao, AddressCityEntity> implements AddressCityService {

    @Override
    public List<AddressCityEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.id");
        params.put("asc", false);
        Page<AddressCityEntity> page = new QueryPlus<AddressCityEntity>(params).getPage();
        return page.setRecords(baseMapper.selectAddressCityPage(page, params));
    }

    @Override
    public boolean add(AddressCityEntity addressCity) {
        return this.save(addressCity);
    }

    @Override
    public boolean update(AddressCityEntity addressCity) {
        UpdateWrapper<AddressCityEntity> updateWrapper = new UpdateWrapper();
        updateWrapper
                .eq("id", addressCity.getId())
                .set("code", addressCity.getCode())
                .set("province_code", addressCity.getProvinceCode())
                .set("name", addressCity.getName())
                .set("short_name", addressCity.getShortName())
                .set("merger_name", addressCity.getMergerName())
                .set("zip_code", addressCity.getZipCode())
                .set("longitude", addressCity.getLongitude())
                .set("latitude", addressCity.getLatitude())
                .set("sort", addressCity.getSort())
                .set("update_at", new Date());
        return super.update(updateWrapper);
    }

    @Override
    public boolean delete(Long id) {
        return this.deleteBatch(new Long[]{id});
    }

    @Override
    public boolean deleteBatch(Long[] ids) {
        if (ArrayUtils.isEmpty(ids)) {
            return false;
        }
        UpdateWrapper<AddressCityEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);
        updateWrapper.set("is_delete", DataValidConstant.IS_DELETE_TRUE);
        return this.update(updateWrapper);
    }

    @Override
    public Map<String, String> getAllCodeMap() {
        String cacheKey = RedisKeysConstant.ADDRESS + ":" + RedisKeysConstant.ADDRESS_CITY_MAP;
        Object dataJson = J2CacheUtils.get(cacheKey);
        if (!StringUtils.isNullOrEmpty(dataJson)) {
            return JSON.parseObject(dataJson.toString(), new TypeReference<Map<String, String>>() {
            });
        }
        List<AddressCityEntity> addressCityList = this.list();
        if (CollectionUtils.isEmpty(addressCityList)) {
            return Collections.emptyMap();
        }
        Map<String, String> map = new HashMap<>();
        for (AddressCityEntity addressCity : addressCityList) {
            map.put(addressCity.getCode(), addressCity.getShortName());
        }
        J2CacheUtils.put(cacheKey, JSON.toJSONString(map));
        return map;
    }
}
